package test.test.one.objects;

import test.test.one.objects.tools.Speed;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;

/**
 * @author Carlos
 * @author Gustavo
 * 
 * This class is called to instance the controllable character object
 * It features animation
 *
 */
public class ob_character extends AbstractObject {
	
	private static final String TAG = ob_character.class.getSimpleName(); //Class TAG
	
	private Speed speed; //the object can move
	//private Bitmap bitmap;		
	private Rect sourceRect; //to select frame	
	private int frameNr; //total number of frames		
	private int currentFrame; //current displayed frame
	private long frameTicker; //time since last update
	private int framePeriod; //time between frames
	private int spriteWidth, spriteHeight; //used to create rectangle		
					
	private ob_pickable inventory[]; //the object stores picked objects in this array
	public int item_counter; //the number of items currently on the inventory
	public boolean inventoryIsOpen; //for displaying purposes
	
	/*
	 * constructor
	 */
	
	/**TODO finish documentation
	 * Basic constructor, no inventory
	 * @param bitmap, sprite image
	 * @param x, x position
	 * @param y, y position
	 * @param width
	 * @param height
	 * @param fps
	 * @param frameCount
	 */
	public ob_character(Bitmap bitmap, int x, int y, int width, int height, int fps, int frameCount) {
		super(bitmap, x, y);
		this.speed = new Speed();
		this.bitmap = bitmap;
		this.x = x;
		this.y = y;
		currentFrame = 0;
		frameNr = frameCount;
		spriteWidth = bitmap.getWidth() / frameCount;
		spriteHeight = bitmap.getHeight()/2;
		sourceRect = new Rect(0, 0, spriteWidth, spriteHeight);
		framePeriod = 1000 / fps;
		frameTicker = 0l;
		
		this.inventory=new ob_pickable [0];
		this.item_counter=0;
	}
	
	/**TODO finish documentation
	 * Advanced constructor, with inventory
	 * @param bitmap
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 * @param fps
	 * @param frameCount
	 * @param inv
	 */
	public ob_character(Bitmap bitmap, int x, int y, int width, int height, int fps, int frameCount, int inv) {
		super(bitmap, x, y);
		this.speed = new Speed();
		this.bitmap = bitmap;
		this.x = x;
		this.y = y;
		currentFrame = 0;
		frameNr = frameCount;
		spriteWidth = bitmap.getWidth() / frameCount;
		spriteHeight = bitmap.getHeight()/2;
		sourceRect = new Rect(0, 0, spriteWidth, spriteHeight);
		framePeriod = 1000 / fps;
		frameTicker = 0l;
		
		this.inventory=new ob_pickable [inv];
		this.item_counter=0;
		this.inventoryIsOpen=false;
	}
	
	/*
	 * get & set methods
	 */
	
	public Rect getSourceRect() {
		return sourceRect;
	}
	public void setSourceRect(Rect sourceRect) {
		this.sourceRect = sourceRect;
	}
	public int getFrameNr() {
		return frameNr;
	}
	public void setFrameNr(int frameNr) {
		this.frameNr = frameNr;
	}
	public int getCurrentFrame() {
		return currentFrame;
	}
	public void setCurrentFrame(int currentFrame) {
		this.currentFrame = currentFrame;
	}
	public int getFramePeriod() {
		return framePeriod;
	}
	public void setFramePeriod(int framePeriod) {
		this.framePeriod = framePeriod;
	}
	public int getSpriteWidth() {
		return spriteWidth;
	}
	public void setSpriteWidth(int spriteWidth) {
		this.spriteWidth = spriteWidth;
	}
	public int getSpriteHeight() {
		return spriteHeight;
	}
	public void setSpriteHeight(int spriteHeight) {
		this.spriteHeight = spriteHeight;
	}	
	public Speed getSpeed() {
		return speed;
	}
	public void setSpeed(Speed speed) {
		this.speed = speed;
	}
	public ob_pickable getInventory(int pos) {
		return inventory[pos];
	}
	public void setInventory(ob_pickable item, int pos) {
		this.inventory[pos] = item;
	}
	
	/*
	 * methods
	 */
	
	/**
	 * this method is called whenever an item is picked
	 * @param ob, the picked item
	 */
	public void addItem(ob_pickable ob){
		inventory[item_counter]=ob;
		item_counter++;
	}
	
	//TODO documentation
	public void update(long gameTime, float posx) {
		if (speed.getxDirection()==-1){
			sourceRect= new Rect(0, spriteHeight, spriteWidth, +spriteHeight+(bitmap.getHeight()/2));}
		
		else {
				
			sourceRect= new Rect(0, 0, spriteWidth, +spriteHeight);
				
			}	
		if (!touched) {
			//Log.d(TAG, ""+x);
			x += (speed.getXv() * speed.getxDirection()); 
			y += (speed.getYv() * speed.getyDirection());
		}
		if (!(posx>x+5 || posx< x -5)){
			//Log.d(TAG, "frame 0 ");
	
			currentFrame = 0;
		}
		else{if (gameTime > frameTicker + framePeriod) {
			frameTicker = gameTime;
			currentFrame++;
			if (currentFrame >= frameNr) {
				currentFrame = 1;
			}}
		}
		this.sourceRect.left = currentFrame * spriteWidth;
		this.sourceRect.right = this.sourceRect.left + spriteWidth;
	}

	//TODO documentation
	public void draw(Canvas canvas) {
		
		//Log.d(TAG,""+speed.getxDirection());
		//int srcy = 1 *spriteHeight;
		
		Rect destRect = new Rect(getX(), getY(), getX() + spriteWidth, getY() + spriteHeight);
		canvas.drawBitmap(bitmap, sourceRect, destRect, null);
	}

	/**
	 * This method was overwritten to due to the nature of an animation Bitmap, 
	 * and how it works with Rectangles
	 * 
	 * @param eventX X-coordinate
	 * @param eventY Y-coordinate
	 * 
	 * @author Carlos
	 */
	public void handleActionDown(int eventX, int eventY) {
		if (eventX >= (x - spriteWidth / 2) && (eventX <= (x + spriteWidth /2 ))) 
			if (eventY >= (y - spriteHeight / 2) && (eventY <= (y + spriteHeight /2 ))) {
				setTouched(true);
				return;
			}
			 Log.d(TAG, "no esta tocando el monito");
			setTouched(false);
		}
	
}

